package com.kanshu.basictest.ui.activity.ipc;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;

import com.kanshu.basictest.R;
import com.kanshu.basictest.constants.PubContants;
import com.kanshu.basictest.model.menu.MenuItem;
import com.kanshu.basictest.ui.base.BaseListMenuActivity;

import java.util.ArrayList;
import java.util.List;

public class MessengerTestActivity extends BaseListMenuActivity implements Handler.Callback {


    private static final String TAG = "MessengerTestActivity";
    private Messenger server;
    //如果需要接受来自服务端的消息
    Handler handler = new Handler(this);
    Messenger client = new Messenger(handler);

    @Override
    protected String provideBarTitle() {
        return "Messenger测试";
    }

    @Override
    protected List<? extends MenuItem> provideMenuData() {
        List<MenuItem> menuItems = new ArrayList<>();
        menuItems.add(0, new MenuItem("向服务端发送信息", R.color.blue_200));
        return menuItems;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {
            case 0:
                sendMsg2Server();
                break;
            default:
                break;
        }

    }

    @Override
    protected void init() {
        super.init();
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.kanshu.basictest", "com.kanshu.basictest.service.MessengerService"));
        bindService(intent, conn, Service.BIND_AUTO_CREATE);
    }

    private final ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i(TAG, "onServiceConnected: 服务端绑定成功");
            server = new Messenger(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            server = null;
        }
    };

    private void sendMsg2Server() {
        if (server != null) {
            Message msg = Message.obtain(null, PubContants.MSG_FROM_CLIENT);
            Bundle data = new Bundle();
            data.putString("msg", "hello,this is client!");
            msg.setData(data);

            msg.replyTo = client;

            try {
                server.send(msg);
            } catch (RemoteException e) {
                e.printStackTrace();
            }


        } else {
            Log.i(TAG, "sendMsg2Server:server == null ");
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case PubContants.MSG_FROM_SERVER:
                //处理来自于服务端的回应
                showPromptDialog("服务端的响应", msg.getData().getString("reply"));

                break;
        }

        return true;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(conn);
    }
}
